<template>
  <el-card style="max-width:500px;margin:40px auto;">
    <h2 style="text-align:center;">个人信息</h2>
    <el-form :model="form" label-width="80px" style="margin-top:20px;">
      <el-form-item label="用户名">
        <el-input v-model="form.username" disabled />
      </el-form-item>
      <el-form-item label="姓名">
        <el-input v-model="form.realName" />
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="form.phone" />
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="form.email" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSave">保存</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>
<script setup>
import { ref } from 'vue'
import { useUserStore } from '@/store/user'
import axios from 'axios'
import { ElMessage } from 'element-plus'
const userStore = useUserStore()
const form = ref({
  username: userStore.userInfo?.username || '',
  realName: userStore.userInfo?.realName || '',
  phone: userStore.userInfo?.phone || '',
  email: userStore.userInfo?.email || ''
})
const onSave = async () => {
  const res = await axios.put(`http://localhost:8081/api/user/${userStore.userInfo.id}`, form.value, {
    headers: { Authorization: `Bearer ${userStore.token}` }
  })
  if (res.data.code === 200) {
    ElMessage.success('保存成功')
    userStore.setUserInfo(res.data.data)
  } else {
    ElMessage.error(res.data.msg)
  }
}
</script> 